package com.btpj.module_main.ui.home

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.btpj.jetpack.base.BaseViewModel
import com.btpj.jetpack.utils.ToastUtil
import com.btpj.jetpack.entity.ApiResult
import com.btpj.module_main.data.entity.Banner
import com.btpj.module_main.data.remote.repository.MainRepository
import kotlinx.coroutines.launch

class HomeViewModel(private val repository: MainRepository) : BaseViewModel() {

    val bannerLiveData = MutableLiveData<List<Banner>>()

    override fun start() {
        getBanner()
    }

    /**
     * 轮播图数据请求
     */
    private fun getBanner() {
        viewModelScope.launch {
            when (val result = repository.getBanner()) {
                is ApiResult.Success -> bannerLiveData.postValue(result.data)
                is ApiResult.Error -> ToastUtil.showShort(
                    result.exception.errorMsg ?: "请求失败"
                )
            }
        }
    }

}